package com.itheima.tlias84.controller;

import com.itheima.tlias84.entity.Stu;
import com.itheima.tlias84.service.StuService;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/stus")
public class StuController {
    @Resource
    StuService stuService;

    @PostMapping
    public ResultUtil insertStudent(Stu student){
        int inserted = stuService.insertStu(student);
        return inserted == 0 ? ResultUtil.fail(inserted) : ResultUtil.success(inserted);
    }

    @DeleteMapping
    public ResultUtil deleteStudentById(Integer id){
        int deleted = stuService.deleteStuById(id);
        return deleted == 0 ? ResultUtil.fail(deleted) : ResultUtil.success(deleted);
    }

    @PutMapping()
    public ResultUtil updateStudentById(Stu student){
        int updated = stuService.updateStuById(student);
        return updated == 0 ? ResultUtil.fail(updated) : ResultUtil.success(updated);
    }

    @GetMapping
    public ResultUtil selectStudentById(Integer id){
        Stu student = stuService.selectStuById(id);
        return ResultUtil.success(student);
    }

    @GetMapping("/all")
    public ResultUtil selectAllStudent(){
        return ResultUtil.success(stuService.selectAllStu());
    }

    @DeleteMapping("/batchDelete")
    public ResultUtil batchDelete(@RequestParam("ids") List<Integer> ids){
        int deleted = stuService.batchDelete(ids);
        return deleted == 0 ? ResultUtil.fail(deleted) : ResultUtil.success(deleted);
    }

    @PutMapping("/batchInsert")
    public ResultUtil insertStudentBatch(@RequestBody List<Stu> list){
        int inserted = stuService.insertStuBatch(list);
        return inserted == 0 ? ResultUtil.fail(inserted) : ResultUtil.success(inserted);
    }
}
